import { Order, Page, User, type PageIn } from '../client'
import { makeRequest } from '../client-core'
import { Comment } from './models'

const request = makeRequest('/copilot/api/v1')

export class Users extends request('/user') {
  static retrieve() {
    return Users.$api.get<User>({ typeof: User })
  }

  static update(data: User) {
    return Users.$api.put({
      data,
      includes: ['username', 'password', 'name', 'avatar', 'description', 'gender', 'phone', 'qrcode']
    })
  }
}

export class Comments extends request('/comment') {
  static list() {
    return Comments.$api.get<Comment[]>({
      transform: (data) => data.map((x: any) => Comment.inst(x))
    })
  }
}

export class Orders extends request('/order') {
  static list(params: PageIn) {
    return Orders.$api.get<Page<Order>>({ params, transform: (data) => Page.inst<any>(data, [Order]) })
  }

  static retrieve(id: number) {
    return Orders.$api.get<Order>({ paths: [id], typeof: Order })
  }
}
